package com.fzy.leetcodeExam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Main test = new Main();
        test.minOperations(new int[]{3,1,6,8},new int[]{1,5});
    }

    public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {

        int[] arr = new int[numOnes+numZeros+numNegOnes];
        int index = 0;
        for(;index<numOnes;index++){
            arr[index] = 1;
        }
        for(;index<numOnes+numZeros;index++){
            arr[index] = 0;
        }
        for(;index<numOnes+numZeros+numNegOnes;index++){
            arr[index] = -1;
        }
        int ans = 0;
        for(int i = 0;i<k;i++){
            ans += arr[i];
        }

        return ans;
    }


    public boolean primeSubOperation(int[] nums) {
        int n = nums.length;
        int[] minPrime = new int[n];

        Arrays.fill(minPrime,Integer.MIN_VALUE);
        boolean[] used = new boolean[n];

        for(int i = 0;i<n;i++){
            for(int j = 2;j<nums[i];j++){
                if(isPrime(j)){
                    minPrime[i] = j;
                    break;
                }
            }
        }
        while (true){

            boolean flag = true;

            for(int i = 0;i<n;i++){
                if(!used[i]){
                    int val = minPrime[i];
                    if(val<Integer.MAX_VALUE && (i == 0 || nums[i] > nums[i-1])){
                        nums[i] -= val;
                        if(nums[i] < nums[i-1]){
                            nums[i] += val;
                            minPrime[i] = val+1;
                        }else {
                            used[i] = true;
                        }
                        flag = !flag;
                    }
                }
            }
            if(flag){
                return true;
            }
        }
    }

    private boolean isPrime(int x){

        if(x<2){
            return false;
        }
        for(int i = 2;i*i <= x;i++){
            if(x%i == 0){
                return false;
            }
        }
        return true;
    }

    public List<Long> minOperations(int[] nums, int[] queries) {
        int len1 = nums.length;
        int len2 = queries.length;

        if(nums == null || nums.length == 0){
            return new ArrayList<Long>();
        }

        if(queries == null || queries.length == 0){
            return new ArrayList<Long>();
        }
        Arrays.sort(nums);
        List<Long> list = new LinkedList<>();
        for(int i = 0;i<len2;i++){
            int[] copynums = Arrays.copyOf(nums,len1);
            int val = queries[i];
            long ans = 0;
            if(copynums[0] >= val){
                ans = Arrays.stream(copynums).sum() - val*len1;
                list.add(ans);
                continue;
            }

            for(int j = 0;j<len1;j++){
                if(copynums[j] < 0){
                    ans -= copynums[j];
                    copynums[j] = 0;
                }
            }
            Arrays.sort(copynums);
            int index = 0;
            for(int j = 0;j<copynums.length;j++){
                if(val < nums[j]){
                    index = j;
                    break;
                }
            }
            for(int j = 0;j<index;j++){
                ans += val - copynums[j];
            }
            for(int j = index;j<copynums.length;j++){
                ans += copynums[j] - val;
            }
            ans = Math.abs(ans);
            list.add(ans);
        }
        return list;
    }

}
